Ext.define('MyApp.view.uploadimage.UploadImageForm', {
	extend : 'Ext.form.Panel',
	xtype : 'uploadimageform',
	bodyPadding : 10,// 内边距
	fieldDefaults : {
		margin : '0 10 0 5',
		allowBlank : false
	},
	ownerEntity : '',
	ownerId : '',
	items : [{
				xtype : 'fieldset',
				layout : 'column',
				title:'图片预览',
				height : 300,
				width :280,
				items:{
					xtype:'image',
					height : 280,
					width :260,
					maxHeight:300,
					maxWidth:280,
					id:'img',
					listeners :{
						'afterrender':function(){
							this.setSrc('uploadimage/showImage/'+this.up('form').ownerEntity+'/'+this.up('form').ownerId);
						}
					}
				}
			}, {
				xtype : 'fieldset',
				layout : 'column',
				border : false,
				items : [{
							xtype : 'filefield',
							name : 'multipartFile',
							hideLabel : true,
							width : 200,
							msgTarget: 'side',
							buttonText : '选择文件',
							emptyText : '请选择文件',
							regex: /\.([jJ][pP][gG]){1}$|\.([jJ][pP][eE][gG]){1}$|\.([gG][iI][fF]){1}$|\.([pP][nN][gG]){1}$|\.([bB][mM][pP]){1}$/,
 							regexText:"图片格式错误",
 							buttonConfig: {
				                glyph:0xf002
				            },
 							listeners:{
			                        change:function(btn, value){
			                            var img_reg = /\.([jJ][pP][gG]){1}$|\.([jJ][pP][eE][gG]){1}$|\.([gG][iI][fF]){1}$|\.([pP][nN][gG]){1}$|\.([bB][mM][pP]){1}$/;
			                            if ( img_reg.test(value) ) {
			                                var img = Ext.getCmp('img');
			                                var file = btn.fileInputEl.dom.files[0];
			                                var url = URL.createObjectURL(file);
			                                img.setSrc(url);
			                            } else {
			                                Ext.Msg.alert('提示', '请选择图片类型的文件！');
			                                return ;
			                            }
			                        }
			                    }
						}, {
							xtype : 'hiddenfield',
							name : 'ownerEntity'
						}, {
							xtype : 'hiddenfield',
							name : 'ownerId'
						}, {
							xtype : 'button',
							text : '上传',
							glyph:0xf093,
							handler : function(btn) {
								var formPanle = btn.up('form'), form = formPanle
										.getForm();
								if (form.isValid()) {
									form.setValues({
												ownerEntity : formPanle.ownerEntity,
												ownerId : formPanle.ownerId
											});
									btn.up('form').getForm().submit({
										url : 'uploadimage/upload',
										submitEmptyText : false,
										waitMsg : '请稍等，图片正在上传!',
										success : function(fp, o) {
											var text = Ext.decode(o.response.responseText);
											if(text.success!='0'){
												Ext.MessageBoxUtil.msg('提示','上传成功!');
											}else{
												Ext.MessageBoxUtil.msg('提示','上传失败!');
											}
										},
										failure :function(form,action){
											Ext.MessageBoxUtil.msg('提示','上传失败,服务器发生错误!');
										}
									});
								}
							}
						}]
			}]
});